package com.bsj.pay.util.platform.wsgw.sm;

import org.bouncycastle.crypto.params.*;
import org.bouncycastle.math.ec.*;
import org.bouncycastle.crypto.*;
import java.math.*;
import org.bouncycastle.util.encoders.*;
import java.io.*;

public class SM2Utils
{
    private static final int BYTE_LENGTH = 32;
    
    public static void generateKeyPair() {
        final SM2 instance = SM2.Instance();
        ECPoint q = null;
        final byte[] array = new byte[32];
        final byte[] array2 = new byte[32];
        for (int i = 0; i < 10; ++i) {
            final AsymmetricCipherKeyPair generateKeyPair = instance.ecc_key_pair_generator.generateKeyPair();
            final ECPrivateKeyParameters ecPrivateKeyParameters = (ECPrivateKeyParameters)generateKeyPair.getPrivate();
            final ECPublicKeyParameters ecPublicKeyParameters = (ECPublicKeyParameters)generateKeyPair.getPublic();
            final BigInteger d = ecPrivateKeyParameters.getD();
            q = ecPublicKeyParameters.getQ();
            final byte[] byteArray = d.toByteArray();
            if (!Util.byteToHex(byteArray).equals(Util.byteToHex(array2))) {
                System.out.println("----------> \u7b2c " + i + " \u5c1d\u8bd5\u751f\u621032\u4f4d\u5bc6\u94a5\uff0c\u5f53\u524d\u5bc6\u94a5\u957f\u5ea6\uff1a" + byteArray.length + " <----------");
                if (byteArray.length == 32) {
                    System.arraycopy(byteArray, 0, array, 0, 32);
                    break;
                }
            }
        }
        System.out.println("SM2\u516c\u94a5: " + Util.byteToHex(q.getEncoded()));
        System.out.println("SM2\u79c1\u94a5: " + Util.byteToHex(array));
    }
    
    public byte[] decrypt(byte[] array, byte[] hexToByte) throws IOException {
        if (array == null || array.length == 0) {
            array = null;
        }
        else if (hexToByte == null || hexToByte.length == 0) {
            array = null;
        }
        else {
            final String byteToHex = Util.byteToHex(hexToByte);
            final byte[] hexToByte2 = Util.hexToByte(byteToHex.substring(0, 130));
            final int n = hexToByte.length - 97;
            hexToByte = Util.hexToByte(byteToHex.substring(194, n * 2 + 194));
            final byte[] hexToByte3 = Util.hexToByte(byteToHex.substring(n * 2 + 130, n * 2 + 194));
            final byte[] hexToByte4 = Util.hexToByte(byteToHex.substring(130, 194));
            final SM2 instance = SM2.Instance();
            final BigInteger bigInteger = new BigInteger(1, array);
            final ECPoint decodePoint = instance.ecc_curve.decodePoint(hexToByte2);
            final Cipher cipher = new Cipher();
            cipher.Init_dec(bigInteger, decodePoint);
            cipher.Decrypt(hexToByte);
            cipher.Dofinal(hexToByte3);
            array = hexToByte;
            if (!new String(Hex.encode(hexToByte3)).equals(new String(Hex.encode(hexToByte4)))) {
                array = null;
            }
        }
        return array;
    }
    
    public byte[] encrypt(final byte[] array, final byte[] array2) throws IOException {
        byte[] hexToByte;
        final byte[] array3 = hexToByte = null;
        if (array != null) {
            if (array.length != 65) {
                hexToByte = array3;
            }
            else {
                hexToByte = array3;
                if (array != null) {
                    hexToByte = array3;
                    if (array.length != 0) {
                        hexToByte = array3;
                        if (array2 != null) {
                            hexToByte = array3;
                            if (array2.length != 0) {
                                final byte[] array4 = new byte[array2.length];
                                System.arraycopy(array2, 0, array4, 0, array2.length);
                                final Cipher cipher = new Cipher();
                                final SM2 instance = SM2.Instance();
                                final ECPoint init_enc = cipher.Init_enc(instance, instance.ecc_curve.decodePoint(array));
                                cipher.Encrypt(array4);
                                final byte[] array5 = new byte[32];
                                cipher.Dofinal(array5);
                                hexToByte = Util.hexToByte(Util.byteToHex(init_enc.getEncoded()) + Util.byteToHex(array5) + Util.byteToHex(array4));
                            }
                        }
                    }
                }
            }
        }
        return hexToByte;
    }
}